<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Music;
use App\Models\MusicParagraph;
use App\Models\MusicParagraphSentence;
use App\Models\MusicParagraphSentenceConfig;
use App\Models\MusicTypeMenu;
use Illuminate\Http\Request;

class MusicParagraphSentenceConfigController extends Controller
{

    public function save(Request $request)
    {

        // 句子
        $id = $request->post("id");

        // 删除所有的段落配置
        // 添加新的段落配置

        MusicParagraphSentenceConfig::where("music_paragraph_sentence_id",$id)->delete();
        // 句子
        $musicParagraphSentenceModel = MusicParagraphSentence::find($id);

        // 段落
        $musicParagraphModel = MusicParagraph::find($musicParagraphSentenceModel->music_paragraph_id);
        // 音乐
        $musicModel = Music::find($musicParagraphModel->music_id);

        $musicTypeMenuModels = MusicTypeMenu::where("music_type_id", $musicModel->music_type_id)
            ->get();
        $arrTmp = [];
//        dd($musicTypeMenuModels);
        foreach ($musicTypeMenuModels as $v){
            $arrTmp[] = [
                "music_paragraph_sentence_id" => $id,
                "music_type_menu_code" => $v->music_type_menu_code,
                "music_paragraph_sentence_file" => $request->post($v->music_type_menu_code),
            ];
        }
//        dd( $arrTmp );
        MusicParagraphSentenceConfig::upsert($arrTmp, [
            ["music_paragraph_sentence_id","music_type_menu_code"],
            ["music_paragraph_sentence_file"]
        ]);


        return redirect()->route("adminMusicParagraphSentenceIndex", ["id" => $musicParagraphSentenceModel->music_paragraph_id]);

    }



    public function edit(Request $request)
    {
        // 句子ID
        $id = $request->input("id");


        $musicParagraphSentenceConfigs = MusicParagraphSentenceConfig::where("music_paragraph_sentence_id", $id)
            ->pluck("music_paragraph_sentence_file","music_type_menu_code");

//        dd($musicParagraphSentenceConfigs);
        $musicParagraphSentenceModel = MusicParagraphSentence::find($id);

        // 段落
        $musicParagraphModel = MusicParagraph::find($musicParagraphSentenceModel->music_paragraph_id);

        // 音乐
        $musicModel = Music::find($musicParagraphModel->music_id);

        $musicTypeMenuModels = MusicTypeMenu::where(
            "music_type_id", $musicModel->music_type_id
        )->get();

//        dump($musicParagraphModel);
//        dump($musicModel);
//        dd($musicTypeMenuModels);
        $pageName = $musicModel->music_name . " " . $musicParagraphModel->music_paragraph_name .  $musicParagraphSentenceModel->music_paragraph_sentences_name ."配置";

        return view("admin.musicParagraphSentenceConfig.edit", [
            "musicTypeMenuModels" => $musicTypeMenuModels,
            "musicParagraphSentenceConfigs" => $musicParagraphSentenceConfigs,
            "musicParagraphSentenceModel" => $musicParagraphSentenceModel,
//            "musicParagraphSentenceModel" => $musicParagraphSentenceModel,
//            "musicModel" => $musicModel,
            "pageName" => $pageName,
//
        ]);
    }

}
